package cc.minos.components.scrollbar
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class ScrollContent extends Sprite implements IScrollAbleV , IScrollAbleH
	{
		protected const MIN_SCROLL_V:int = 1;
		protected const MIN_SCROLL_H:int = 0;
		
		private var _compositionWidth:Number;
		private var _compositionHeight:Number;
		
		protected var content:DisplayObject;
		
		public function ScrollContent( w:Number , h:Number )
		{
			super();
			
			compositionWidth = w;
			compositionHeight = h;
			
			content = this;
			
			addEventListener( MouseEvent.MOUSE_WHEEL , onMouseWheel );
		}
		
		/* INTERFACE cc.minos.components.scrollbar.IScrollAbleH */
		
		public function get scrollH():Number
		{
			return MIN_SCROLL_H - content.x;
		}
		
		public function set scrollH( value:Number ):void
		{
			var tmp:Number = scrollH;
			
			if ( value < MIN_SCROLL_H )
				value = MIN_SCROLL_H;
			else if ( value > maxScrollH )
				value = maxScrollH;
			
			content.x = MIN_SCROLL_H - value;
			if ( value != tmp )
				dispatchEvent( new Event( Event.SCROLL ) );
		}
		
		public function get maxScrollH():Number
		{
			var val:Number;
			val = MIN_SCROLL_H + 1 + content.width - _compositionWidth;
			return val < 0 ? 0 : val;
		}
		
		public function get contentWidth():Number
		{
			return content.width;
		}
		
		/* INTERFACE cc.minos.components.scrollbar.IScrollAbleV */
		
		public function get scrollV():Number
		{
			return MIN_SCROLL_V - content.y;
		}
		
		public function set scrollV( value:Number ):void
		{
			var tmp:Number = scrollV;
			if ( value < MIN_SCROLL_V )
				value = MIN_SCROLL_V;
			else if ( value > maxScrollV )
				value = maxScrollV;
			
			content.y = MIN_SCROLL_V - value;
			if ( value != tmp )
				dispatchEvent( new Event( Event.SCROLL ) );
		}
		
		public function get maxScrollV():Number
		{
			var val:Number;
			val = MIN_SCROLL_V + 1 + content.height - _compositionHeight;
			return val < 1 ? 1 : val;
		}
		
		public function get contentHeight():Number
		{
			return content.height;
		}
		
		public function get compositionHeight():Number
		{
			return _compositionHeight;
		}
		
		public function set compositionHeight( value:Number ):void
		{
			_compositionHeight = value;
		}
		
		public function get compositionWidth():Number
		{
			return _compositionWidth;
		}
		
		public function set compositionWidth( value:Number ):void
		{
			_compositionWidth = value;
		}
		
		protected function onMouseWheel( e:MouseEvent ):void
		{
			var value:Number = scrollV - e.delta * 10;
			scrollV = value;
			e.updateAfterEvent();
		}
	
	}

}